package com.zxy.ziems.server.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.domain.DictItem;
import com.zxy.ziems.server.dto.DictItemDto;
import com.zxy.ziems.server.dto.DictItemQueryDto;
import com.zxy.ziems.server.service.DictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 所有字典项
 *
 * @author bhg
 * @date 2023/3/13
 */
@RestController
@RequestMapping("/api/dictItem")
public class DictItemController {
    @Autowired
    private DictItemService dictItemService;

    /**
     * 分页列表
     * @param dictItemQueryDto
     * @return
     */
    @PostMapping("/page/list")
    public JsonResponse<PageResult<DictItem>> pageList (@RequestBody DictItemQueryDto dictItemQueryDto) {
        PageResult<DictItem> pageResult = dictItemService.pageList(dictItemQueryDto);
        return JsonResponse.SUCCESS(pageResult);
    }

    /**
     * 字典项列表
     * @param dictItemQueryDto
     * @return
     */
    @PostMapping("/list")
    public JsonResponse<List<DictItem>> list (@RequestBody DictItemQueryDto dictItemQueryDto) {
        List<DictItem> pageResult = dictItemService.list(dictItemQueryDto);
        return JsonResponse.SUCCESS(pageResult);
    }

    /**
     * 新增修改字段项
     * @param dictItemDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse addOrUpdate (@RequestBody@Validated DictItemDto dictItemDto) {
        dictItemService.addOrUpdate(dictItemDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 启动停止字段项
     * @param dictItemDto
     * @return
     */
    @PostMapping("/startOrStop")
    public JsonResponse startOrStop (@RequestBody DictItemDto dictItemDto) {
        dictItemService.startOrStop(dictItemDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 删除字段项
     * @param itemId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse delete(@RequestParam(value = "itemId") String itemId) {
        dictItemService.removeById(itemId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 字段项
     * @return
     */
    @GetMapping("/itemCollection")
    public JsonResponse<Map<String, List<DictItem>>> itemCollection () {
        Map<String, List<DictItem>> map = dictItemService.itemCollection();
        return JsonResponse.SUCCESS(map);
    }

    /**
     * 获取字典有效项
     * @param code
     * @return
     */
    @GetMapping("/getByDictCode/{code}")
    public JsonResponse<List<DictItem>> getByDictCode(@PathVariable String code) {
        return JsonResponse.SUCCESS(dictItemService.getByDictCode(code));
    }
    /**
     * 获取字典有效项
     * @param code
     * @return
     */
    @GetMapping("/getByDictCode/{code}/{itemName}")
    public JsonResponse<DictItem> getByDictCodeAndItemName(@PathVariable String code, @PathVariable String itemName) {
        return JsonResponse.SUCCESS(dictItemService.getByDictCodeAndItemName(code, itemName));
    }
}
